/**
 * 
 */
package cn.core.autobuilder.service;

import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.core.autobuilder.dao.mapper.Builder_codeMapper;
import cn.core.autobuilder.dao.mapper.Builder_tableMapper;
import cn.core.autobuilder.dao.po.Builder_codePO;
import cn.core.autobuilder.dao.po.Builder_tablePO;
import cn.osworks.aos.builder.db.DbTableInfo;
import cn.osworks.aos.builder.metainfo.DBMetaInfoUtils;
import cn.osworks.aos.builder.metainfo.vo.TableVO;
import cn.osworks.aos.core.asset.AOSCons;
import cn.osworks.aos.core.asset.AOSUtils;
import cn.osworks.aos.core.dao.SqlDao;
import cn.osworks.aos.core.exception.AOSException;
import cn.osworks.aos.core.id.AOSId;
import cn.osworks.aos.core.typewrap.Dto;
import cn.osworks.aos.core.typewrap.Dtos;

/**
 * @author penghe hepeng1@163.com
 *  
 */
@Service
public class Builer_codeService {
	@Autowired
	private Builder_codeMapper builder_codemapper;
	@Autowired
	private Builder_tableMapper builder_tablemapper;
	@Autowired
	private SqlDao sqlDao;
	@Transactional
	public Dto saveBuiler_code(Dto inDto) {
		Dto outDto = Dtos.newDto();
		Builder_codePO builder_codePO = new Builder_codePO();
		AOSUtils.copyProperties(inDto, builder_codePO);
		if (!checkIsExist(builder_codePO.getTable_name_(),builder_codePO.getColumn_name_() )) {
			outDto.setAppCode(AOSCons.ERROR);
			String msg = AOSUtils.merge("[{0}][{1}]已被存在, 请重新输入。", builder_codePO.getTable_name_(),builder_codePO.getColumn_name_());
			outDto.setAppMsg(msg);
			return outDto;
		}
		builder_codePO.setId_(AOSId.uuid());
		builder_codemapper.insert(builder_codePO);
		outDto.setAppCode(AOSCons.SUCCESS);
		outDto.setAppMsg("操作完成，新增成功。");
		return outDto;
	}
	@Transactional
	public Dto updateBuiler_code(Dto inDto) {
		Dto outDto = Dtos.newDto();
		Builder_codePO builder_codePO = new Builder_codePO();
		AOSUtils.copyProperties(inDto, builder_codePO);
		builder_codemapper.updateByKey(builder_codePO);
		outDto.setAppCode(AOSCons.SUCCESS);
		outDto.setAppMsg("操作完成，更新成功。");
		return outDto;
	}
	@Transactional
	public Dto delete(Dto qDto) {
		Dto outDto = Dtos.newDto();
		String[] selections = qDto.getRows();
		int del = 0;
		for (String id_ : selections) {
			builder_codemapper.deleteByKey(id_);
			del++;
		}
		String msg = "操作完成, ";
		if (del > 0) {
			msg = AOSUtils.merge(msg + "成功删除[{0}]个。", del);
		}
		sqlDao.delete("Builder_table.deletebuilder_tableNotinbuilder_code", qDto);
		sqlDao.delete("Builder_code.deletebuilder_codeNotinbuilder_table", qDto);
		outDto.setAppMsg(msg);
		return outDto;
	}
	public boolean checkIsExist(String table_name_,String column_name_) {
		Dto calcDto = Dtos.newCalcDto("COUNT(id_)");
		calcDto.put("table_name_", table_name_);
		calcDto.put("column_name_", column_name_);
		Integer countInteger = Integer.valueOf(builder_codemapper.calc(calcDto));
		boolean out = true;
		if (countInteger > 0) {
			out = false;
		}
		return out;
	}
	public boolean checkTableIsExist(String table_name_,String db_name_) {
		Dto calcDto = Dtos.newCalcDto("COUNT(id_)");
		calcDto.put("table_name_", table_name_);
		calcDto.put("db_name_", db_name_);
		Integer countInteger = Integer.valueOf(builder_tablemapper.calc(calcDto));
		boolean out = true;
		if (countInteger > 0) {
			out = false;
		}
		return out;
	}
	
	@Transactional
	public Dto importtableinfo(Dto inDto) {
		Dto outDto = Dtos.newDto();
		DbTableInfo dbtableinfo = new DbTableInfo();
		String db_name_ = inDto.getString("db_name_");
		String table_name_ = inDto.getString("table_name_");
		Dto pDto = Dtos.newDto();
		pDto.put("db_name_", db_name_);
		pDto.put("table_name_", table_name_);
		inDto.put("table_des_", dbtableinfo.getTabelInfoByComment(DBMetaInfoUtils.newConnection(), inDto).get("table_comment"));
		ArrayList<Dto> talbelist=dbtableinfo.getTabelInfo(DBMetaInfoUtils.newConnection(), inDto);
		if(AOSUtils.isEmpty(talbelist)){
			outDto.setAppCode(AOSCons.ERROR);
			outDto.setAppMsg("导入表失败，表不存在请检查！");
			return outDto;
		}
		if(!checkTableIsExist(table_name_, db_name_)){   //存在删除tabel
			List<Builder_tablePO> builder_tableList=builder_tablemapper.list(pDto);
	        for (Builder_tablePO builder_tablepo : builder_tableList) {
	        	builder_tablemapper.deleteByKey(builder_tablepo.getId_());		
			}
		}
		Builder_tablePO builder_tablePO = new Builder_tablePO();
		AOSUtils.copyProperties(inDto, builder_tablePO);
		builder_tablePO.setId_(AOSId.uuid());
		builder_tablemapper.insert(builder_tablePO);
		
		List<Builder_codePO> builder_code=builder_codemapper.list(pDto);
		for (Builder_codePO builder_codepo : builder_code) {
			 builder_codemapper.deleteByKey(builder_codepo.getId_());
		}
	 
		for (Dto dbinfo : talbelist) {
			Builder_codePO builder_codePO = new Builder_codePO();
			AOSUtils.copyProperties(dbinfo, builder_codePO);
			builder_codePO.setId_(AOSId.uuid());
			builder_codemapper.insert(builder_codePO);
		}
		sqlDao.delete("Builder_table.deletebuilder_tableNotinbuilder_code", inDto);
		sqlDao.delete("Builder_code.deletebuilder_codeNotinbuilder_table", inDto);
		outDto.setAppCode(AOSCons.SUCCESS);
		outDto.setAppMsg("操作完成，导入数据成功,请到表功能管理里面维护字段属性，进入下一步生成DAO");
		return outDto;
	}
	@Transactional
	public Dto importtableinfos(Dto inDto) throws SQLException {
		Dto outDto = Dtos.newDto();
		inDto.put("package", inDto.getString("package_"));
		inDto.put("author", inDto.getString("author"));
		String[] tables = inDto.getRows();
	    DatabaseMetaData databaseMetaData = DBMetaInfoUtils.getDatabaseMetaData();
		for (String tableName : tables) {
			inDto.put("table_name_", tableName);
			inDto.put("db_name_", inDto.getString("db_name_").split(",")[0]);
			importtableinfo(inDto);
			TableVO tableVO = DBMetaInfoUtils.getTableVO(databaseMetaData, tableName);
			if (AOSUtils.isEmpty(tableVO)) {
				throw new AOSException("表[" + tableName + "]不存在。");
			}
		}
		outDto.setAppCode(AOSCons.SUCCESS);
		outDto.setAppMsg("操作完成，导入数据成功,请到表功能管理里面维护字段属性，进入下一步生成DAO");
		return outDto;
	}
	
	@Transactional
	public Dto updateBuiler_table(Dto inDto) {
		Dto outDto = Dtos.newDto();
		Builder_tablePO builder_tablepo = new Builder_tablePO();
		AOSUtils.copyProperties(inDto, builder_tablepo);
		builder_tablemapper.updateByKey(builder_tablepo);
		outDto.setAppCode(AOSCons.SUCCESS);
		outDto.setAppMsg("操作完成，更新成功。");
		return outDto;
	}
	
}
